package 队列;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class 哈夫曼树 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Integer[] a = new Integer[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        PriorityQueue<Integer> b = new PriorityQueue<>(Arrays.asList(a));

        int sum = 0;

        while (b.size()>1){
            int c = b.poll()+b.poll();
            b.offer(c);
            sum = sum + c ;
        }
        System.out.println(sum);




    }
}